package com.ruix.app.model;

import android.text.TextUtils;

import com.ruix.app.manager.UserManager;
import com.ruix.app.util.DateUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by hadexs on 2017/4/19.
 */

public class PostsCard implements Serializable{
    private String ID;   //
    private String words; //
    private long createdTime; //
    private String createdTimeDisplay;
    private String uid;  //uid
    private String userName; //
    private String avatarUrl; //
    private String roleID;
    private int likeCnt;
    private long CommentCnt;
    private ArrayList<Praise> likes;
    private ArrayList<Comment> comments;
    private ArrayList<String> album; //图片
    private PostsType displayType; //展示类型
//    private String displayLikes;
    private boolean hasPraisedByUser;
    private String encryptId ;//


    public PostsCard parse(JSONObject object) {
        if (object != null) {
            ID = object.optString("id");
            encryptId = object.optString("encrypt_id");
            words = object.optString("words", "");
            createdTime = object.optLong("created_at",0)*1000;
            buildeDisplayTime();
            uid = object.optString("uid");
            userName = object.optString("username");
            avatarUrl = object.optString("avatar");
            roleID = object.optString("role_id");
            JSONArray commentsArray = object.optJSONArray("comments");
            if (commentsArray != null && commentsArray.length() > 0) {
                comments = new ArrayList<Comment>();
                for (int i = 0; i < commentsArray.length(); i++) {
                    JSONObject commentObject = commentsArray.optJSONObject(i);
                    Comment comment = new Comment().parse(commentObject);
                    comments.add(comment);
                }
            }
            JSONArray likesArray = object.optJSONArray("likes");
            if (likesArray != null && likesArray.length() > 0) {
                likes = new ArrayList<Praise>();
                String uid =  UserManager.getCurrentUser().getID();
                for (int i = 0; i < likesArray.length(); i++) {
                    JSONObject likeObject = likesArray.optJSONObject(i);
                    Praise zan = new Praise().parse(likeObject);
                    likes.add(zan);
                    if (zan.getActorUid().equals(uid)){
                        hasPraisedByUser = true;
                    }
                }
            }
            JSONArray albumArray = object.optJSONArray("album");
            if (albumArray != null && albumArray.length() > 0) {
                album = new ArrayList();
                for (int i = 0; i < albumArray.length(); i++) {
                    JSONObject albumobj =  albumArray.optJSONObject(i);
                    album.add(albumobj.optString("url", ""));
                }
            }
            caculateDisplayType();

        }

        return this;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }

    public long getCreatedTime() {
        return createdTime;
    }


    public void setCreatedTime(long createdTime) {
        this.createdTime = createdTime;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getRoleID() {
        return roleID;
    }

    public void setRoleID(String roleID) {
        this.roleID = roleID;
    }

    public int getLikeCnt() {
        return likeCnt;
    }

    public void setLikeCnt(int likeCnt) {
        this.likeCnt = likeCnt;
    }

    public long getCommentCnt() {
        return CommentCnt;
    }

    public void setCommentCnt(long commentCnt) {
        CommentCnt = commentCnt;
    }

    public ArrayList<Praise> getLikes() {
        return likes;
    }

    public void setLikes(ArrayList<Praise> likes) {
        this.likes = likes;
    }

    public ArrayList<Comment> getComments() {
        return comments;
    }

    public void setComments(ArrayList<Comment> comments) {
        this.comments = comments;
    }

    public void repaceComments(ArrayList<Comment> comments){
        this.comments = comments;
    }

    public ArrayList<String> getAlbum() {
        return album;
    }

    public void setAlbum(ArrayList album) {
        this.album = album;
    }

    public String getCreatedTimeDisplay() {
        return createdTimeDisplay;
    }

    public String getEncryptId() {
        if (!TextUtils.isEmpty(encryptId)){
            try {
                return URLEncoder.encode(encryptId,"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return encryptId;
    }

    public void setEncryptId(String encryptId) {
        this.encryptId = encryptId;
    }

    public void setCreatedTimeDisplay(String createdTimeDisplay) {
        this.createdTimeDisplay = createdTimeDisplay;
    }

    public void buildeDisplayTime() {
        createdTimeDisplay= "";
        if (createdTime > 0) {
            createdTimeDisplay= DateUtil.createMsgTimeStyle(createdTime);
        }
    }

    public void caculateDisplayType() {
        if (album != null && album.size() > 0) {
            int size = album.size();
            if (size == 1) {
                displayType = PostsType.POSTS_TYPE_ONE;
            } else if (size == 2) {
                displayType = PostsType.POSTS_TYPE_TWO;
            } else if (size == 3) {
                displayType = PostsType.POSTS_TYPE_THREE;
            } else if (size == 4) {
                displayType = PostsType.POSTS_TYPE_FOUR;
            } else if (size == 5 || size == 6) {
                displayType = PostsType.POSTS_TYPE_FIVE;
            } else if (size == 9 || size == 7 || size == 8) {
                displayType = PostsType.POSTS_TYPE_SIX;
            } else {
                displayType = PostsType.POSTS_TYPE_SIX;
            }
        } else {
            displayType = PostsType.POSTS_TYPE_TEXT;
        }

    }

    public PostsType getDisplayType() {
        return displayType;
    }

//    public String getLikesName(){
//        if (likes != null  && likes.size() > 0){
//            for (int i = 0; i < likes.size() )
//        }
//
//        return ;
//    }



    public boolean isHasPraisedByUser() {
        return hasPraisedByUser;
    }

    //点赞
    public void addPraise(){
        hasPraisedByUser = true;
        Praise praise = new Praise();
        User user = UserManager.getCurrentUser();
        praise.setActorUid(user.getID());
        praise.setActorName(user.getName());
        if (likes == null) {
            likes = new ArrayList<>();
        }
        likes.add(0, praise);
    }

    //取消
    public void canclePraise(){
        hasPraisedByUser = false;
        if (likes != null){
            String uid =  UserManager.getCurrentUser().getID();
            if (uid != null) {
                for (int i = 0; i < likes.size(); i++) {
                    Praise praise = likes.get(i);
                    if (praise != null && praise.getActorUid().equals(uid)) {
                        likes.remove(i);
                        break;
                    }
                }
            }
        }
    }

    public void deleteComment(int position) {
        ArrayList<Comment> comments = getComments();
        if (comments != null && comments.size() > position) {
            comments.remove(position);
        }
    }

    public void addComment(Comment comment){
        ArrayList<Comment> comments = getComments();
        if (comments == null) {
            comments = new ArrayList<>();
        }
        comments.add(comment);
    }

    public String getAlbumWithIndex(int i){
        return getAlbum().get(i) + "?x-oss-process=style/thumb_medium";
    }

    public String getAlbumSmallWithIndex(int i){
        return getAlbum().get(i) + "?x-oss-process=style/thumb_small";
    }

    public String authorRoleValue(boolean isTeacherCircle){
        if (!TextUtils.isEmpty(roleID)) {
            if (Role.RoleMentor.role().equals(roleID)){
                return Role.RoleMentor.value();
            } else if (Role.RoleSignPics.role().equals(roleID)){
                return Role.RoleSignPics.value();
            } else if (Role.RoleGuest.role().equals(roleID)){
                return Role.RoleGuest.value();
            } else if (Role.RoleAdviser.role().equals(roleID)){
                if (isTeacherCircle){
                    return Role.RoleMentor.value();
                }
                return Role.RoleAdviser.value();
            } else if (Role.RoleCommittee.role().equals(roleID)){
                return Role.RoleCommittee.value();
            } else if (Role.RoleMonitor.role().equals(roleID)){
                return Role.RoleMonitor.value();
            } else if (Role.RoleSchoolmate.role().equals(roleID)){
                return null;
            }
        }

        return null;
    }



    public String authorRoleValue(String roleID){
        if (!TextUtils.isEmpty(roleID)) {
            if ("1".equals(roleID)){
                return "导师";
            } else if ("5".equals(roleID)){
                return "小助手";
            } else if ("6".equals(roleID)){
                return "嘉宾";
            } else if ("8".equals(roleID)){
                return "班主任";
            } else if ("9".equals(roleID)){
                return "班委";
            } else if ("10".equals(roleID)){
                return "班长";
            }
        }
        return null;
    }
}
